package com.yunxin.core.infra.persistence.repository.order;

import com.yunxin.core.application.admin.team.Team;
import com.yunxin.core.domain.model.order.OrderAllocationBelongToAccount;
import com.yunxin.core.domain.model.order.OrderAllocationBelongToAccountRepository;
import com.yunxin.core.infra.persistence.converter.OrderConverter;
import com.yunxin.core.infra.persistence.entity.order.OrderAllocationBelongToAccountDO;
import com.yunxin.core.infra.persistence.mapper.order.OrderAllocationBelongToAccountExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class OrderAllocationBelongToAccountRepositoryImpl implements OrderAllocationBelongToAccountRepository {

    private final OrderAllocationBelongToAccountExtendMapper orderAllocationBelongToAccountExtendMapper;

    private final OrderConverter orderConverter;

    @Override
    public boolean saveOrderAllocationBelongToAccount(OrderAllocationBelongToAccount orderAllocationBelongToAccount) {
        OrderAllocationBelongToAccountDO orderAllocationBelongToAccountDO = orderConverter.formOrderAllocationBelongToAccount(orderAllocationBelongToAccount);
        int count = orderAllocationBelongToAccountExtendMapper.insertSelective(orderAllocationBelongToAccountDO);
        return count > 0;
    }

    @Override
    public boolean updateOrderAllocationBelongToAccount(OrderAllocationBelongToAccount orderAllocationBelongToAccount) {
        OrderAllocationBelongToAccountDO orderAllocationBelongToAccountDO = orderConverter.formOrderAllocationBelongToAccount(orderAllocationBelongToAccount);
        int count = orderAllocationBelongToAccountExtendMapper.updateByPrimaryKey(orderAllocationBelongToAccountDO);
        return count > 0;
    }

    @Override
    public OrderAllocationBelongToAccount getOrderAllocationBelongToAccountByAllocationOrderId(Long allocationOrderId) {
        OrderAllocationBelongToAccountDO orderAllocationBelongToAccountDO = orderAllocationBelongToAccountExtendMapper.selectByAllocationOrderId(allocationOrderId);
        return orderConverter.toOrderAllocationBelongToAccount(orderAllocationBelongToAccountDO);
    }


    /**
     * 查询已分配的团队信息
     *
     * @param allocationOrderId
     * @return
     */
    @Override
    public List<Team> selecteamName(Long allocationOrderId) {
        return orderAllocationBelongToAccountExtendMapper.selectTeamName(allocationOrderId);
    }


    /**
     * 根据已有团队查询未分配团队
     *
     * @param teamIds
     * @return
     */
    @Override
    public List<Team> selecteamNameNo(List<Integer> teamIds) {
        List<Team> teams = orderAllocationBelongToAccountExtendMapper.selecteamNameNo(teamIds);
        return teams.isEmpty() ? null : teams;
    }

    /**
     * 查询所有团队信息
     *
     * @return
     */
    @Override
    public List<Team> selectAllTeamName() {
        return orderAllocationBelongToAccountExtendMapper.selectAllTeamName();
    }
}
